﻿using DART.Detector.IRay;
using DART.Detector.IRay.Wrapper;
using Dumpify;
using DynamicData;
using DynamicData.Binding;
using System.Reactive.Linq;

namespace DART.IRay.Test
{
    internal class Program
    {

        static async Task Main(string[] args)
        {
            await EventAsync();
            Console.ReadLine();
        }

        /// <summary>
        /// 事件方式
        /// </summary>
        /// <returns></returns>
        static async Task EventAsync()
        {
            try
            {
                using (var detector = IRayDetector.Instance)
                {
                    // 设置探测器参数
                    detector.Param.SetWorkDirectory(Path.Combine(AppContext.BaseDirectory, "IRay/Config/Mammo1012X")); // 工作目录，IRay专用设置
                    detector.Param[nameof(IRayAttr.UROMContinuCapFrameNumW)] = 25; // TOMO帧数，IRay专用设置
                    detector.Param[nameof(IRayAttr.UROMFrameNumberW)] = 2; // 多帧叠加的帧数，IRay专用设置

                    detector.DetectorEvent += (s, e) =>
                    {
                        var iray = e.DeviceArgs as IRayEventArgs; // IRay 设备底层事件
                        Console.WriteLine($"[{DateTime.Now:yyyy/MM/dd HH:mm:ss.ffff}] {iray}");
                    };
                    // Event to Observable
                    var observable = Observable.FromEventPattern<DetectorEventArgs>(h => detector.DetectorEvent += h, h => detector.DetectorEvent -= h);
                    
                    // 初始化探测器，写入探测器参数
                    await detector.InitAsync();

                    // 获取探测器信息 输出到控制台
                    (await detector.GetInfoAsync()).DumpConsole();

                    // 设置探测器为 Contact AEC Filter1 Prep
                    await detector.SetAcqAsync(new AcqParam
                    {
                        Mode = DetectorMode.Contact,
                        AutoExposure = true,
                        Filter = (int)Filter.Filter1,
                        [nameof(TriggerMode)] = TriggerMode.Prep, // Prep模式，IRay专用设置
                    });

                    Console.WriteLine("请触发预曝光采集...");

                    // 等待预曝光完成，并获取预曝光图像
                    var image0 = await observable.Select(o => o.EventArgs.Image as IRayImage).FirstAsync(i => i != null && i.IsPreExposure);

                    // 设置多帧叠加采集，500ms 2帧
                    await detector.SetParamAsync(new Dictionary<string, object> { ["Time"] = 500, ["Frame"] = 2 }); ///////施工中/////////

                    Console.WriteLine("请触发500ms主曝光...");

                    // 等待主曝光完成，并获取主曝光图像
                    var image1 = await observable.Select(o => o.EventArgs.Image as IRayImage).FirstAsync(i => i != null && !i.IsPreExposure);

                    Console.WriteLine("曝光完成.");

                    // 等待序列计数更新（Mammo_Flow_End 曝光流程完成）
                    await detector.Counter.WhenValueChanged(c => c.SequenceCount).FirstAsync(s => s == 1);

                    Console.WriteLine("采集结束.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}
